package com.easyjava.builder;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BuildService
 * @Date 2024/7/30  15:11
 **/
public class BuildService {

    private static final Logger logger = LoggerFactory.getLogger(BuildService.class);

    public static void execute(TableInfo tableInfo) {


        File folder = new File(Constants.PATH_SERVICE);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String className = tableInfo.getBeanName()+Constants.SUFFIX_SERVICE;
        File file = new File(folder, className + ".java");


        try {
            OutputStream outputStream = Files.newOutputStream(file.toPath());
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "utf8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("package " + Constants.PACKAGE_SERVICE + ";");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            bufferedWriter.write("import java.util.List;");
            bufferedWriter.newLine();
            bufferedWriter.write("import com.easyjava.vo.PaginationResultVO;");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_PO + "."+ tableInfo.getBeanName()+";");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_QUERY + "." +tableInfo.getBeanParamName() +";");
            bufferedWriter.newLine();
            BuildComment.createClassComment(bufferedWriter, tableInfo.getComment() + "Service");
            bufferedWriter.write("public interface " + className + "{");

            bufferedWriter.newLine();
            BuildComment.createFieldComment(bufferedWriter,"根据条件查询列表");
            bufferedWriter.write("\tList<" +tableInfo.getBeanName() +"> findListByParam("+ tableInfo.getBeanParamName() + " param);");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            BuildComment.createFieldComment(bufferedWriter,"根据条件查询数量");
            bufferedWriter.write("\tint findCountByParam" + "(" + tableInfo.getBeanParamName() + " param);");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            BuildComment.createFieldComment(bufferedWriter,"分页查询");
            bufferedWriter.write("\tPaginationResultVO<"+tableInfo.getBeanName()+"> findListByPage(" + tableInfo.getBeanParamName()+ " param);");
            bufferedWriter.newLine();
            bufferedWriter.newLine();

            BuildComment.createFieldComment(bufferedWriter,"新增");
            bufferedWriter.write("\tint add("+tableInfo.getBeanName()+" bean);");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            BuildComment.createFieldComment(bufferedWriter,"批量新增");
            bufferedWriter.write("\tint addBatch(List<"+tableInfo.getBeanName()+"> listBean);");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            BuildComment.createFieldComment(bufferedWriter,"批量新增或修改");
            bufferedWriter.write("\tint addOrUpdateBatch(List<"+tableInfo.getBeanName()+"> listBean);");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            for (Map.Entry<String, List<FieldInfo>> entry :  tableInfo.getKeyIndexMap().entrySet()) {
                List<FieldInfo> keyFieldInfoList = entry.getValue();
                int index = 0;
                StringBuilder methodName = new StringBuilder();
                StringBuilder methodParams = new StringBuilder();
                for (FieldInfo fieldInfo : keyFieldInfoList) {
                    index++;
                    methodName.append(StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName()));
                    if (index < keyFieldInfoList.size()) {
                        methodName.append("And");
                    }
                    methodParams.append(fieldInfo.getJavaType()).append(" ").append(fieldInfo.getPropertyName());
                    if (index < keyFieldInfoList.size()) {
                        methodParams.append(", ");
                    }
                }
                BuildComment.createFieldComment(bufferedWriter,"根据"+ methodName + "查询");
                bufferedWriter.write("\t"+tableInfo.getBeanName()+" get" + tableInfo.getBeanName()+ "By" + methodName + "("+methodParams+");");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                BuildComment.createFieldComment(bufferedWriter,"根据"+ methodName + "更新");
                bufferedWriter.write("\tint update" + tableInfo.getBeanName()+ "By" + methodName + "("+tableInfo.getBeanName() + " bean, " + methodParams + ");");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                BuildComment.createFieldComment(bufferedWriter,"根据"+ methodName + "删除");
                bufferedWriter.write("\tint delete" + tableInfo.getBeanName()+ "By" + methodName +"("+methodParams+");");
                bufferedWriter.newLine();
                bufferedWriter.newLine();
            }

            bufferedWriter.write("}");
            bufferedWriter.flush();
        } catch (Exception e) {
            logger.error("创建service失败", e);
        }
    }
}
